<?php

namespace App\Models\Trip\OrderPay;

use App\Models\Task\SnapTasks\SnapTask;
use App\Models\Train\TrainOrders\TrainOrder;
use Eloquent;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

/**
 * App\Models\Trip\OrderPay\SnapTaskEditPay
 *
 * @property int                $id
 * @property string             $task_id             要编辑的任务id
 * @property string             $order_sn            订单编号
 * @property string             $pay_sn              支付单号
 * @property string             $before_order_amount 改之前的订单金额
 * @property string             $after_order_amount  改之后的订单金额
 * @property string             $pay_amount          需要支付的订单金额
 * @property mixed              $trip_info           行程数据,用于回调时下单
 * @property Carbon|null        $created_at
 * @property Carbon|null        $updated_at
 * @method static Builder|SnapTaskEditPay newModelQuery()
 * @method static Builder|SnapTaskEditPay newQuery()
 * @method static Builder|SnapTaskEditPay query()
 * @method static Builder|SnapTaskEditPay whereAfterOrderAmount($value)
 * @method static Builder|SnapTaskEditPay whereBeforeOrderAmount($value)
 * @method static Builder|SnapTaskEditPay whereCreatedAt($value)
 * @method static Builder|SnapTaskEditPay whereId($value)
 * @method static Builder|SnapTaskEditPay whereOrderSn($value)
 * @method static Builder|SnapTaskEditPay wherePayAmount($value)
 * @method static Builder|SnapTaskEditPay wherePaySn($value)
 * @method static Builder|SnapTaskEditPay whereTaskId($value)
 * @method static Builder|SnapTaskEditPay whereTripInfo($value)
 * @method static Builder|SnapTaskEditPay whereUpdatedAt($value)
 * @property string $trip_key MD5 json 唯一 关键字key
 * @property-read OrderPay|null $order_pay
 * @method static Builder|SnapTaskEditPay whereTripKey($value)
 * @mixin Eloquent
 */
class SnapTaskEditPay extends Model
{
    use HasFactory;

    protected $table = "snap_task_edit_pay";
    protected $fillable
                     = [
            "task_id",
            "order_sn",
            "pay_sn",
            "trip_info",
            "trip_key",
            "before_order_amount",
            "after_order_amount",
            "pay_amount",
        ];

    public function order_pay()
    {
        return $this->hasOne(OrderPay::class, 'pay_sn', 'pay_sn');
    }

    public function getTripInfo(): string
    {
        return $this->getAttribute('trip_info');
    }

    public function listByTaskAndTrainOrder(SnapTask $task, TrainOrder $order_info): Collection
    {
        return $this->where('task_id', $task->getTaskId())
                    ->where('order_sn', $order_info->getOrderSn())
                    ->get();
    }

    public function getTaskId(): string
    {
        return $this->getAttribute('task_id');
    }

    public function getOrderSn(): string
    {
        return $this->getAttribute('order_sn');
    }

    /**
     * @throws Exception
     */
    public function addPay(OrderPay $pay, array $data): self
    {
        // trip_key
        if ($this->findOneByTripKey($data['trip_key'])) {
            throw new Exception("本次更新已存在");
        }

        return $this->create($data);
    }

    public function findOneByTripKey(string $trip_key): ?SnapTaskEditPay
    {
        return $this->findOneBy(['trip_key' => $trip_key]);
    }

    public function findOneBy(array $where): ?self
    {
        return $this->where($where)->first();
    }

    public function findOneByOrderPay(OrderPay $order_pay): ?self
    {
        return $this->findOneBy([
            'pay_sn' => $order_pay->getPaySn(),
        ]);
    }

    public function getPaySn(): string
    {
        return $this->getAttribute('pay_sn');
    }
}
